﻿using System;

namespace Levfier.Proccessors.Filters
{
    /// <summary>
    /// ハイパスフィルタ（IIR)
    /// </summary>
    [Serializable]
    public class HighPassIIR : IIR
    {
        /// <summary>
        /// フィルタ係数を初期化する
        /// </summary>
        /// <param name="sender">イベント発生源</param>
        protected override void InitFilterCoefficient(object sender)
        {
            //デジタル周波数 -> アナログ周波数に変換
            double analogFreq = (Math.Tan(Math.PI * this.cutOffFrequencyParam.Value / VSTParameterManager.GetInstance().SampleRate) / (2.0d * Math.PI));

            //共通の値を計算 2pifc
            double tempCommon = (2.0d * Math.PI * analogFreq);
            //4pi^2fc^2
            double tempCommon2 = tempCommon * tempCommon;

            //係数計算
            this.b0 = 1.0d / (1.0d + (tempCommon / this.qualityFactorParam.Value) + tempCommon2);
            this.b1 = -2.0d * this.b0;
            this.b2 = this.b0;
            this.a1 = 2.0d * (tempCommon2 - 1.0d) / (1.0d + (tempCommon / this.qualityFactorParam.Value) + tempCommon2);
            this.a2 = (1.0d - (tempCommon / this.qualityFactorParam.Value) + tempCommon2) / (1.0d + (tempCommon / this.qualityFactorParam.Value) + tempCommon2);
        }
    }
}
